package 贪心算法;

import java.util.Arrays;
import java.util.Comparator;

public class 无重叠区间 {

    public static int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });

        int n = intervals.length;
        int right = intervals[0][1];
        int num = 0;
        for (int i = 1; i < n; i++) {
            if (intervals[i][0] >= right){
                right = intervals[i][1];
            }else {
                num++;
            }
        }
        return num;
    }

    public static void main(String[] args) {
        int[][] intervals = {{1,2},{1,3},{2,3},{3,4}};
        System.out.println(eraseOverlapIntervals(intervals));
    }
}
